<template>
  <div class="container">
    <div class="app-container" style="padding: 20px 20px;">
      <el-button size="mini" type="primary" @click="isShowDialog=true">添加区域</el-button>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="序号">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="区域名称" prop="areaName" />
        <el-table-column label="车位数 (个)" prop="spaceNumber" />
        <el-table-column label="面积(m²)" prop="areaProportion" />
        <el-table-column label="计费规则" prop="ruleName" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" size="medium" @click="edit(row)">编辑</el-button>
            <el-button type="text" size="medium" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" align="middle" style="margin-top: 20px;">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <el-dialog :visible.sync="isShowDialog" width="35%" @close="closeDialog">
      <el-form ref="addForm" :model="addForm" :rules="addRules" label-width="120px">
        <el-form-item label="区域名称" prop="areaName">
          <el-input v-model="addForm.areaName" placeholder="请输入区域名称" size="small" style="width: 80%;" />
        </el-form-item>
        <el-form-item label="车位数（个）" prop="spaceNumber">
          <el-input v-model="addForm.spaceNumber" placeholder="请输入车位个数" size="small" style="width: 80%;" />
        </el-form-item>
        <el-form-item label="面积（m²）" prop="areaProportion">
          <el-input v-model="addForm.areaProportion" placeholder="请输入面积" size="small" style="width: 80%;">
            <template slot="append">m²</template>
          </el-input>
        </el-form-item>
        <el-form-item label="关联计费规则" prop="ruleId">
          <el-select v-model="addForm.ruleId" placeholder="请选择" size="small" style="width: 80%;">
            <el-option
              v-for="item in dropList"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addForm.remark" type="textarea" :rows="3" placeholder="请输入备注" size="small" style="width: 80%;" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="text" @click="isShowDialog = false">取消</el-button>
        <el-button size="mini" type="primary" style="margin-left: 25px;" @click="btnOk">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addAreaAPI, delAreaAPI, editAreaAPI, getAreaDropListAPI, getAreaListAPI } from '@/api'

export default {
  name: 'AreaManagement',
  data() {
    return {
      loading: true,
      list: [],
      total: 0,
      currentPage: 1,
      page: 1,
      pageSize: 10,
      isShowDialog: false,
      addForm: {
        areaName: '',
        spaceNumber: '',
        areaProportion: '',
        ruleId: '',
        remark: ''
      },
      addRules: {
        areaName: [
          { required: true, message: '请输入区域名称!', trigger: 'blur' }
        ],
        spaceNumber: [
          { required: true, message: '请输入车位数!', trigger: 'blur' }
        ],
        areaProportion: [
          { required: true, message: '请输入面积!', trigger: 'blur' }
        ],
        ruleId: [
          { required: true, message: '请选择关联计费规则!', trigger: 'blur' }
        ]
      },
      dropList: []
    }
  },
  created() {
    this.getAreaList()
    this.getAreaDropList()
  },
  methods: {
    async getAreaList() {
      const { rows, total } = await getAreaListAPI({ page: this.page, pageSize: this.pageSize })
      this.list = rows
      this.total = total
      this.loading = false
    },
    async getAreaDropList() {
      const { rows } = await getAreaDropListAPI()
      this.dropList = rows
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.getAreaList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.getAreaList()
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await editAreaAPI(this.addForm)
      } else {
        await addAreaAPI(this.addForm)
      }
      this.isShowDialog = false
      this.getAreaList()
    },
    closeDialog() {
      this.$refs.addForm.resetFields()
    },
    async edit(row) {
      this.isShowDialog = true
      this.addForm = row
    },
    async del(id) {
      await this.$confirm('确认删除吗？')
      await delAreaAPI(id)
      this.getAreaList()
    }
  }
}
</script>
